package com.example.myapplication;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    ImageView messageImage,friendImage,circleImage,meImage;
    private List<Friend> friendList = new ArrayList<>();

    LinearLayout wechat,friend,circle,me;

    @Override
    public <T extends View> T findViewById(int id) {
        return super.findViewById(id);
    }

    // 事务管理器
    FragmentManager fm = getSupportFragmentManager();

    Fragment messageFragment = new MessageFragment();
    Fragment friendFragment = new FriendFragment();
    Fragment circleFragment = new CircleFragment();
    Fragment meFragment = new MeFragment();

    @NonNull
    @Override
    public FragmentManager getSupportFragmentManager() {
        return super.getSupportFragmentManager();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.layout_main);
        // 开启事务、添加fragment、提交事务
        FragmentTransaction ft = fm.beginTransaction();
        ft.add(R.id.content, messageFragment);
        ft.add(R.id.content, friendFragment);
        ft.add(R.id.content, circleFragment);
        ft.add(R.id.content, meFragment);
        hideAll(ft);
        // 隐藏所有fragment
        ft.commit();

        // 绑定控件
        wechat =findViewById(R.id.wechat);
        friend = findViewById(R.id.friend);
        circle = findViewById(R.id.circle);
        me = findViewById(R.id.me);
        //  添加点击监听
        wechat.setOnClickListener(this);
        friend.setOnClickListener(this);
        circle.setOnClickListener(this);
        me.setOnClickListener(this);

        // 关联图片
        messageImage = findViewById(R.id.wechatImg);
        friendImage = findViewById(R.id.friendImg);
        circleImage = findViewById(R.id.circleImg);
        meImage = findViewById(R.id.meImg);

        // RecyclerView
        // 数据初始化



    }

    private void hideAll(FragmentTransaction ft) {
        ft.hide(messageFragment);
        ft.hide(friendFragment);
        ft.hide(circleFragment);
        ft.hide(meFragment);
    }

    private void deactivateAllImage(){
        // 将所有图片设置为未激活
        messageImage.setImageResource(R.drawable.wechat);
        friendImage.setImageResource(R.drawable.friends);
        circleImage.setImageResource(R.drawable.color_palette);
        meImage.setImageResource(R.drawable.user);
    }
    @Override
    public void onClick(View v) {
        // 开启事务
        FragmentTransaction fragmentTransaction = fm.beginTransaction();
        // 隐藏所有的fragment
        hideAll(fragmentTransaction);
        deactivateAllImage();
        // 判断点击的是哪个LinearLayout
        switch (v.getId()) {
            case R.id.wechat:
                fragmentTransaction.show(this.messageFragment);
                // 将图片设置为激活
                messageImage.setImageResource(R.drawable.wechat_active);
                break;
            case R.id.friend:
                fragmentTransaction.show(this.friendFragment);
                friendImage.setImageResource(R.drawable.friends_active);
                break;
            case R.id.circle:
                fragmentTransaction.show(this.circleFragment);
                circleImage.setImageResource(R.drawable.color_active);
                break;
            case R.id.me:
                fragmentTransaction.show(this.meFragment);
                meImage.setImageResource(R.drawable.user_active);
                break;
        }
        // 提交事务
        fragmentTransaction.commit();
    }
    private void initFriends() {
        for (int i = 0; i < 2; i++) {
            Friend zhangsan = new Friend("张三","爱跳舞", R.drawable.avator01);
            friendList.add(zhangsan);
            Friend lisi = new Friend("李四","爱唱歌", R.drawable.avator02);
            friendList.add(lisi);
            Friend wangwu = new Friend("王五","爱跑步", R.drawable.avator03);
            friendList.add(wangwu);
            Friend zhaoliu = new Friend("赵六","爱淋雨", R.drawable.avator04);
            friendList.add(zhaoliu);
            Friend zhouqi = new Friend("周七","爱走路", R.drawable.avator05);
            friendList.add(zhouqi);
            Friend a = new Friend("李华","不爱唱歌", R.drawable.avator06);
            friendList.add(a);
            Friend b = new Friend("刘肆","不爱跳舞", R.drawable.avator07);

        }
    }
}